﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InstituteManager.Data;
using System.Data;

namespace InstituteManager.Bussiness
{
    public static class CompetitionBL
    {
        public static string errorMsg = "";
        public static List<tbl_Competition> getByAll()
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            List<tbl_Competition> compe = InsMana.tbl_Competition.ToList();
            return compe;
        }

        public static tbl_Competition getById(int Id)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            tbl_Competition com = new tbl_Competition();
            com = InsMana.tbl_Competition.Include("tbl_Award").Include("tbl_Staff").First(temp => temp.CompetitionId == Id);
            try
            {
               com.tbl_Staff = InsMana.tbl_Staff.First(temp => temp.StaffId == Id);
            }
            catch (Exception)
            {
            }
            return com;
        }

        public static bool AddNew(tbl_Competition com, int StaffId, int AwardId)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                com.tbl_Staff = InsMana.tbl_Staff.First(temp => temp.StaffId == StaffId);
                com.tbl_Award = InsMana.tbl_Award.First(temp => temp.AwardId == AwardId);
                InsMana.AddTotbl_Competition(com);
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message + e.StackTrace;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool Update(tbl_Competition com, int StaffId, int AwardId)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                tbl_Competition compe = InsMana.tbl_Competition.First(temp => temp.CompetitionId == com.CompetitionId);
                compe.CompetitionName = com.CompetitionName;
                compe.StartDate = com.StartDate;
                compe.EndDate = com.EndDate;
                compe.Condition = com.Condition;
                compe.tbl_Staff = InsMana.tbl_Staff.First(temp => temp.StaffId == StaffId);
                compe.tbl_Award = InsMana.tbl_Award.First(temp => temp.AwardId == AwardId);
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool Delete(int Id)
        {
            
            try
            {  

                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                tbl_Competition com = InsMana.tbl_Competition.Include("tbl_Staff").Include("tbl_Award").Include("tbl_Posting").First(temp => temp.CompetitionId == Id);
                if (com.tbl_Posting.Count > 0)
                {
                    errorMsg = "You can't delete this Competition ! This Competition have some Posting is submitted ! ";
                    return false;
                }
                InsMana.DeleteObject(com);
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static List<tbl_Competition> getUpcomming()
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return (from sc in InsMana.tbl_Competition where sc.StartDate > DateTime.Now select sc).ToList<tbl_Competition>();
        }

        public static List<tbl_Competition> getOngoing()
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return (from sc in InsMana.tbl_Competition where sc.StartDate < DateTime.Now && DateTime.Now < sc.EndDate select sc).ToList<tbl_Competition>();
        }

        public static List<tbl_Competition> getCompetitionByStatus(int StatusId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            switch (StatusId)
            {
                case 1:
                    return (from sc in InsMana.tbl_Competition where sc.StartDate > DateTime.Now select sc).ToList<tbl_Competition>();
                case 2:
                    return (from sc in InsMana.tbl_Competition where sc.StartDate < DateTime.Now && sc.EndDate > DateTime.Now select sc).ToList<tbl_Competition>();
                case 3:
                    return (from sc in InsMana.tbl_Competition where sc.EndDate < DateTime.Now select sc).ToList<tbl_Competition>();
            }
            return (from sc in InsMana.tbl_Competition select sc).ToList<tbl_Competition>();
        }

        

        public static bool isExpired(int CompetitionId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            if ((from sc in InsMana.tbl_Competition where sc.CompetitionId == CompetitionId && sc.EndDate > DateTime.Now select sc).Count() > 0)
                return false;
            return true;
        }

        public static List<tbl_Competition> SearchByName(string CompetitionName)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();

            return (from sc in InsMana.tbl_Competition where sc.CompetitionName.Contains(CompetitionName) select sc).ToList<tbl_Competition>();
        }

        public static string getCompetitionStatusName(int CompetitionId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            tbl_Competition CompeObj = getById(CompetitionId);
            if (CompeObj.StartDate > DateTime.Now)
                return "Upcoming";
            if (CompeObj.StartDate < DateTime.Now && CompeObj.EndDate > DateTime.Now)
                return "Ongoing";
            if (CompeObj.EndDate < DateTime.Now)
                return "Finished";
            return "";
        }

        public static Object SubmissionEvaluationView(string Username)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return (from sc in InsMana.tbl_Competition.Include("tbl_Posting").Include("tbl_Staff") where sc.tbl_Staff.Username == Username select new { CompetitionId = sc.CompetitionId, CompetitionName = sc.CompetitionName, StartDate = sc.StartDate, EndDate = sc.EndDate, NoOfPost = sc.tbl_Posting.Count});
        }

        public static List<tbl_Posting> getListCompetitionPosting(int CompetitionId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return InsMana.tbl_Competition.Include("tbl_Posting").First(temp => temp.CompetitionId == CompetitionId).tbl_Posting.ToList();
        }

        public static int getNumberOfCompetitionPosting(int CompetitionId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return InsMana.tbl_Competition.Include("tbl_Posting").First(temp => temp.CompetitionId == CompetitionId).tbl_Posting.ToList().Count;
        }

        public static bool isMarkAllPost(int CompetitionId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            if ((from sc in InsMana.tbl_Posting.Include("tbl_Competition") where sc.tbl_Competition.CompetitionId == CompetitionId && (sc.Mark == 0 || sc.Mark == null) select sc).Count() > 0)
                return false;
            return true;
        }

        public static bool isAwardIssued(int CompetitionId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            if ((from sc in InsMana.tbl_AwardIssued.Include("tbl_Posting").Include("tbl_Competition") where sc.tbl_Posting.tbl_Competition.CompetitionId == CompetitionId select sc).Count() < 1)
                return false;
            return true;
        }

        public static bool RewardCompetition(int CompetitionId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            List<tbl_Posting> posts = (from sc in InsMana.tbl_Posting.Include("tbl_Competition") where sc.tbl_Competition.CompetitionId == CompetitionId && sc.Mark == 1 select sc).ToList();
            if (posts.Count == 1)
            {
                Data.tbl_AwardIssued obj = new Data.tbl_AwardIssued();
                obj.tbl_Posting = posts[0];
                InsMana.SaveChanges();
            }
            else if (posts.Count > 1)
            {
                errorMsg = "This Competition have more than a Posting have Best Mark ! You need mark only one Best Mark !";
                return false;
            }
            else
            {
                errorMsg = "This Competition haven't got a Best Mark ! Please make a Best Mark to reward !";
                return false;
            }
            return true;
        }

        public static tbl_Posting getPostingAwarded(int CompetitionId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return (from sc in InsMana.tbl_AwardIssued.Include("tbl_Posting").Include("tbl_Competition") where sc.tbl_Posting.tbl_Competition.CompetitionId == CompetitionId select sc.tbl_Posting).First();
            
        }
    }
}
